#include "drv_us015.h"

float g_preDistance_mm[US015_MAX_CH];

float drv_us015_get_distance_mm(uint32_t us015Channel)
{
    float distance_mm;
    int pulseWidth;

    pulseWidth = bsp_us015_icu_io_get_pulseWidth(us015Channel);
    
    distance_mm = pulseWidth*17;
    distance_mm /= 100;

    if(distance_mm<MAX_DISTANCE_MM){
        g_preDistance_mm[us015Channel]=distance_mm;
        return distance_mm;
    }
    else{
        return g_preDistance_mm[us015Channel];
    }
}

void drv_us015_main_function(void)
{
    static uint16_t tick1ms_us015_trigger=0;

    tick1ms_us015_trigger++;

    switch(tick1ms_us015_trigger){
    case 10:
        LL_GPIO_SetOutputPin(US015_TRIGGER_GPIO_Port, US015_TRIGGER_Pin);
        break;
    case 30:
        LL_GPIO_ResetOutputPin(US015_TRIGGER_GPIO_Port, US015_TRIGGER_Pin);
        break;
    case 500:
        tick1ms_us015_trigger=0;
        break;
    default:break;
    }   
}